package com.cwt_taxi_xa.protocol.card.cmd;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.TTsFunction;
import com.cwt_taxi_xa.util.Tools;
/**
 * 驾驶员高分辨率照片文件
 * @author Administrator
 * 文件空间:16384
 */
public class CardPictureEF05 extends BaseCmd<AckCallBack> {

	final String TAG = "CardPictureEF05";
	private int spaceSize = 16384;
	//总包大小
	private int pcfileSize = 0;
	//当前数据包
	private int fileDataIndex = 0;

	private String filePath = "/mnt/sdcard/testFileEF05.jpg";

	public CardPictureEF05(String cmdTag) {
		super(cmdTag);
		// TODO Auto-generated constructor stub
	}



	public CardPictureEF05(String cmdTag, AckCallBack ackCallBack) {
		super(cmdTag, ackCallBack);
		// TODO Auto-generated constructor stub
	}



	@Override
	public String data() {
		// TODO Auto-generated method stub
		fileDataIndex = 0;
		pcfileSize = 0;
		cacheData = null;
		return "02 EF05 01 0000 " + Tools.parseByte2HexStr(Tools.int2Bytes2(2, 2));
	}

	public String data(int start,int size) {
		// TODO Auto-generated method stub
		//
		return "02 EF05 01 "+ Tools.parseByte2HexStr(Tools.int2Bytes2(start, 2)) +" " +  Tools.parseByte2HexStr(Tools.int2Bytes2(size, 2));//0104[260] /022C[556 ]
	}
	// 01 2186 0104 65
	//07-03 16:52:50.123: D/CardPictureEF05(24343): decode smallBytes2Int lenght:59455
	//07-03 16:52:50.123: D/CardPictureEF05(24343): decode bigBytes2Int 	lenght:16360
	//16384


	@Override
	public void decode(byte[] order) {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"decode order:" + (order == null? "null" : order.length));
		if(order != null && order.length == 2){
			int smallInt =  smallBytes2Int(order);
			int bigInt =  bigBytes2Int(order);
			AppLog.savelog("decode smallInt length:" + smallInt + " bigInt 	length:" + bigInt);
			AppLog.d(TAG,"decode smallInt length:" + smallInt + " bigInt 	length:" + bigInt);
			if(smallInt < spaceSize){
				pcfileSize = smallInt;
			}else if(bigInt < spaceSize){
				pcfileSize = bigInt;
			}else{
				pcfileSize = spaceSize - 2;
				arrayIsEmpty = true;
				//saveCacheData(order);
			}
			//pcfileSize = smallBytes2Int(order) < spaceSize ? smallBytes2Int(order) : bigBytes2Int(order);
			readOnePackage();
			AppLog.d(TAG,"读卡-驾驶员高分辨率照片-开始");
		}else{
			if(arrayIsEmpty && arrayIsNull(order)){
				AppLog.d(TAG,"符合非空判断,执行解析数据");
				saveAndMoveNext();
			}else{
				saveCacheData(order);
				readOnePackage();
			}
		}

	}

	private void readOnePackage(){
		AppLog.d(TAG,"readOnePackage fileDataIndex:" + fileDataIndex + " count:" + (pcfileSize/dataLength) + " dataLength:" + dataLength);
		if(fileDataIndex <pcfileSize/dataLength){
			ackCallBack.sendCmd(cmd(),data(2 + fileDataIndex * dataLength,dataLength));//读取第一个包
			fileDataIndex ++ ;
			if(fileDataIndex % 10 == 0){
				TTsFunction.speakText("正在读取照片，请不要移动卡片");
			}
		}else if(fileDataIndex <= (pcfileSize/dataLength)){
			//此次可以取模来处理
			ackCallBack.sendCmd(cmd(),data(2 + fileDataIndex * dataLength,pcfileSize - (fileDataIndex * dataLength)));//读取最后一包
			fileDataIndex ++ ;
		}else{
			saveAndMoveNext();
		}
	}

	private void saveAndMoveNext(){
		AppLog.e(TAG,"读卡照片结束");
		//AppLog.saveFile(data, end, id);
		saveFile(filePath,cacheData);
		MainApplication.topActivity.sendMsg(10088, filePath);
		ackCallBack.moveNext();
	}
}
